package com.example.gulimall.member.service.impl;

import com.example.gulimall.member.entity.MemberLevelEntity;
import com.example.gulimall.member.exception.PhoneNumExistException;
import com.example.gulimall.member.exception.UserNameExistException;
import com.example.gulimall.member.service.MemberLevelService;
import com.example.gulimall.member.vo.LoginVo;
import com.example.gulimall.member.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.member.dao.MemberDao;
import com.example.gulimall.member.entity.MemberEntity;
import com.example.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelService memberLevelService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(RegisterVo registerVo) {
        checkUserName(registerVo.getUserName());
        checkPhoneNum(registerVo.getPhone());
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setMobile(registerVo.getPhone());
        memberEntity.setUsername(registerVo.getUserName());
        memberEntity.setNickname(registerVo.getUserName());
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        String encode = encoder.encode(registerVo.getPassword());
        memberEntity.setPassword(encode);
        MemberLevelEntity memberLevelEntity =memberLevelService.getDefaultLevel();
        memberEntity.setLevelId(memberLevelEntity.getId());
        baseMapper.insert(memberEntity);
    }

    @Override
    public MemberEntity login(LoginVo vo) {
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));
        if(memberEntity!=null){
            String entityPassword = memberEntity.getPassword();
            BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
            boolean matches = encoder.matches(vo.getPassword(), entityPassword);
            if(matches){
                memberEntity.setPassword("");
                return memberEntity;
            }
        }
        return null;
    }

    private void checkPhoneNum(String phone) {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(count>0){
            throw new PhoneNumExistException();
        }
    }

    private void checkUserName(String userName) {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if(count>0){
            throw new UserNameExistException();
        }
    }

}